package gro.ecorrdata.edata.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

// import org.springframework.web.bind.annotation.CrossOrigin;
// import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import gro.ecorrdata.edata.annotation.NoneAuth;
import gro.ecorrdata.edata.model.BaseResponse;
import gro.ecorrdata.edata.model.EDataClassModel;
import gro.ecorrdata.edata.util.MyUtil;

@RestController
// @CrossOrigin 
@RequestMapping("/class")
public class EDataClassController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @NoneAuth
    @RequestMapping("/getdataclasstree")
    public BaseResponse<List<Map<String, Object>>> getEDataClassTree() {
        List<Map<String, Object>> result =  getEDataClassListTree();

        return new BaseResponse<>(true, "查询成功", result);
    }

    public List<Map<String, Object>> getEDataClassListTree() {
        String sql = "select id,pid,classname,tablename,onclick,FROM_UNIXTIME(create_time, '%Y-%m-%d') as ctime,note,infos from edata_class where isdel=0";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
 
        List<Map<String, Object>> result =  MyUtil.makeClassListTree(list, "170");

        return result;
    }
    
    @RequestMapping("/getedataclass")
    public BaseResponse<Map<String, Object>> getEDataClass(Integer id) {
        String sql = "select id,pid,classname,tablename,onclick,FROM_UNIXTIME(create_time, '%Y-%m-%d') as ctime,note from edata_class where id=?";
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> map = jdbcTemplate.queryForMap(sql, id);
            List<Map<String, Object>> cList = getEDataClassListTree();
            result.put("edata", map);
            result.put("cList", cList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BaseResponse<>(true, "查询成功", result);
    }

    public EDataClassModel getEDataClassById(Integer id) {
        String sql = "select id,pid,classname,tablename,onclick,FROM_UNIXTIME(create_time, '%Y-%m-%d') as ctime,note from edata_class where id=?";
        try {
            Map<String, Object> map = jdbcTemplate.queryForMap(sql, id);
            EDataClassModel eDataClass = new EDataClassModel((Integer) map.get("id"), (Integer) map.get("pid"), (String) map.get("classname"),
                (String) map.get("tablename"), (Integer) map.get("onclick"), (String) map.get("ctime"), (String) map.get("note"));
            
            return eDataClass;
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping("/update")
    @PostMapping
    public BaseResponse<Object> updateEDataClass(HttpServletRequest request) {
        boolean result = false;
        String id = request.getParameter("id");
        String classname = request.getParameter("classname");
        if (classname==null || classname== "") {
            return new BaseResponse<>(false, "栏目名不能为空", null);
        }
        Integer pid = (request.getParameter("pid")==null || request.getParameter("pid")=="") ? 0 : Integer.parseInt(request.getParameter("pid"));
        String tablename = (request.getParameter("tablename")==null || request.getParameter("tablename")=="") ? "news" : request.getParameter("tablename");
        Integer onclick = (request.getParameter("onclick")==null || request.getParameter("onclick")=="") ? 0 : Integer.parseInt(request.getParameter("onclick"));
        String note = (request.getParameter("note")==null || request.getParameter("note")=="") ? "" : request.getParameter("note");
        Long ctime = (Long) (System.currentTimeMillis()/1000);
        if (id == null || id=="") {  // 添加
            
            String sql = "INSERT INTO edata_class(pid, classname, tablename, onclick, create_time, note) VALUES(?,?,?,?,?,?)";
            
            int itemp = jdbcTemplate.update(sql,new Object[]{pid, classname, tablename, onclick, ctime, note});

            if (itemp > 0) {
                result =true;
            }   
        } else {   // 修改
            String sql = "UPDATE edata_class SET pid=?, classname=?, tablename=?, onclick=?, note=?, modify_time=?  where `id`=?";

            int itemp = jdbcTemplate.update(sql, new Object[]{pid, classname, tablename, onclick, note, ctime, id});

            if (itemp > 0) {
                result =true;
            }   
        }

        if (result==true) {
            System.out.println("Ok!");
            return new BaseResponse<>(true, "操作成功!", null);
        } else {
            System.out.println("Error!");
            return new BaseResponse<>(false, "操作失败!", null);
        }
    }

    @RequestMapping("/delete")
    @PostMapping
    public BaseResponse<Object> deleteEdataClass(Integer id) {
        boolean result = false;
        if (id == null) {
            return new BaseResponse<>(false, "找不到要删除的数据", null);
        }

        String sql = "SELECT count(*) as counts FROM edata_class WHERE isdel=0 and pid=?";

        Map<String, Object> res = jdbcTemplate.queryForMap(sql, id);

        Long count = (Long) res.get("counts");
        // System.out.println(count);

        if (count!=0) {
            return new BaseResponse<>(false, "子栏目不为空不可删除", null);
        }

        String sql2 = "UPDATE edata_class SET `isdel`=1 where `id`=?";
        // System.out.println(sql);
        // System.out.println(id);
        try {
            jdbcTemplate.update(sql2, id);
            
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return new BaseResponse<>(result, "操作成功", null);
    }

    public List<Map<String, Object>> getEDataClassListByPid(Integer pid) {
        String sql = "select id,pid,classname,tablename from edata_class where isdel=0 and pid=?";
        try {
            List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, pid);
    
            return list;
        } catch (Exception e) {
            return null;
        }
    }

    @NoneAuth
    @RequestMapping("/getclasslistbypid")
    public BaseResponse<List<Map<String, Object>>> getClassListByPid(Integer pid) {
        List<Map<String, Object>> list = getEDataClassListByPid(pid);

        return new BaseResponse<>(true, "查询成功", list);
    }
}